package top.infopub.mgr.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hr.dao.PostGroupDao;
import top.infopub.mgr.hr.service.PostGroupService;
import top.infopub.mgr.hr.service.PostInfoService;
import top.infopub.mgr.sys.service.OperatorService;
import top.infopub.model.hr.PostGroup;
import top.infopub.model.hr.PostGroupInfo;
import top.infopub.model.hr.PostInfo;
import top.infopub.model.sys.Operator;
import top.infopub.security.util.SessionUserUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PostGroupServiceImpl extends GenericServiceImpl<PostGroup, String> implements PostGroupService {

    @Autowired
    private PostGroupDao postGroupDao;

    @Autowired
    private PostInfoService postInfoService;

    @Autowired
    private OperatorService operatorService;

    @Override
    public GenericDao<PostGroup, String> getDao() {
        return postGroupDao;
    }

    @Override
    public void save(PostGroup postGroup) {
        if(StringUtils.isEmpty(postGroup.getId())){
            postGroup.setState(1);//默认可用
            postGroup.setAddBy(SessionUserUtils.getSessionUserId());
            postGroup.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

            postGroupDao.insert(postGroup);

        }else{
            PostGroup postGroup1 = postGroupDao.selectByPrimaryKey(postGroup.getId());
            postGroup1.setUptBy(SessionUserUtils.getSessionUserId());
            postGroup1.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            postGroup1.setCode(postGroup.getCode());
            postGroup1.setName(postGroup.getName());
            postGroup1.setType(postGroup.getType());
            postGroup1.setRoleType(postGroup.getRoleType());
            postGroup1.setMemo(postGroup.getMemo());
            postGroupDao.updateByPrimaryKey(postGroup1);
            //postGroupDao.deleteGroupInfoByGroupId(postGroup.getId());

        }
        /*if(StringUtils.isNotEmpty(postGroup.getPostIds())){
            List<PostGroupInfo> postGroupInfos=new ArrayList<>();
            String[] split = postGroup.getPostIds().split(",");
            for (int i = 0; i < split.length; i++) {
                PostGroupInfo postGroupInfo=new PostGroupInfo();
                postGroupInfo.setGroupId(postGroup.getId());
                postGroupInfo.setPostId(split[i]);
                postGroupInfo.setId(UUID.randomUUID().toString());
                postGroupInfos.add(postGroupInfo);
            }
            postGroupDao.insertByBatch(postGroupInfos);
        }*/
    }

    @Override
    public Page<PostGroup> selectPageByDomainWhere(Page<PostGroup> page, PostGroup postGroup) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PostGroup>)postGroupDao.selectByDomainWhere(postGroup);
    }

    @Override
    public void savePost(String postName, String groupId,String type,String roleType) {
        postName="%"+postName+"%";
        List<PostGroupInfo> postGroupInfos = postGroupDao.selectGroupInfoByGroupId(groupId);
        List<PostGroupInfo> list=new ArrayList<>();
        if("1".equals(roleType)){
            if("4".equals(type)){
                type="";
            }
            List<PostInfo> postInfos = postInfoService.selectPostByName(postName,type);
            if(postInfos!=null&&postInfos.size()>0){
                for (PostInfo postInfo : postInfos) {
                    boolean isExist=true;
                    for (PostGroupInfo exist : postGroupInfos) {
                        if(exist.getPostId().equals(postInfo.getId())){
                            isExist=false;
                        }
                    }
                    if(isExist){
                        PostGroupInfo postGroupInfo=new PostGroupInfo();
                        postGroupInfo.setId(UUID.randomUUID().toString().replace("-", ""));
                        postGroupInfo.setPostId(postInfo.getId());
                        postGroupInfo.setGroupId(groupId);
                        postGroupInfo.setRoleType(roleType);
                        list.add(postGroupInfo);
                    }

                }
                if(list.size()>0){
                    postGroupDao.insertByBatch(list);
                }

            }
        }else{
            Operator po=new Operator();
            po.setSrealName(postName);
            List<Operator> operators = operatorService.selectByEntityWhere(po);
            if(operators!=null&&operators.size()>0){
                for (Operator operator : operators) {
                    boolean isExist=true;
                    for (PostGroupInfo exist : postGroupInfos) {
                        if(exist.getPostId().equals(operator.getId())){
                            isExist=false;
                        }
                    }
                    if(isExist){
                        PostGroupInfo postGroupInfo=new PostGroupInfo();
                        postGroupInfo.setId(UUID.randomUUID().toString().replace("-", ""));
                        postGroupInfo.setPostId(operator.getId());
                        postGroupInfo.setGroupId(groupId);
                        postGroupInfo.setRoleType(roleType);
                        list.add(postGroupInfo);
                    }

                }
                if(list.size()>0){
                    postGroupDao.insertByBatch(list);
                }

            }
        }

    }

    @Override
    public void deleteGroupInfoById(String id) {
        postGroupDao.deleteGroupInfoById(id);
    }
}
